package top.lyc829.zzl_android.ui.index.serach;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.bean.DynamicBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.bean.TeamSimpleBean;
import top.lyc829.zzl_android.logic.bean.TravelsSimpleBean;
import top.lyc829.zzl_android.logic.bean.UserInfoBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class SearchDetialViewModel extends ViewModel {

    private MutableLiveData<List<BaseBean>> baseLiveData;
    private MutableLiveData<List<TravelsSimpleBean>> travelLiveData;
    private MutableLiveData<List<TeamSimpleBean>> teamLiveData;
    private MutableLiveData<List<DynamicBean>> dynamicLiveData;
    private MutableLiveData<List<UserInfoBean>> userLiveData;

    public MutableLiveData<List<BaseBean>> getBaseLiveData() {
        if (baseLiveData == null) {
            baseLiveData = new MutableLiveData<>();
        }
        return baseLiveData;
    }

    public MutableLiveData<List<DynamicBean>> getDynamicLiveData() {
        if (dynamicLiveData == null) {
            dynamicLiveData = new MutableLiveData<>();
        }
        return dynamicLiveData;
    }

    public MutableLiveData<List<TeamSimpleBean>> getTeamLiveData() {
        if (teamLiveData == null) {
            teamLiveData = new MutableLiveData<>();
        }
        return teamLiveData;
    }

    public MutableLiveData<List<TravelsSimpleBean>> getTravelLiveData() {
        if (travelLiveData == null) {
            travelLiveData = new MutableLiveData<>();
        }
        return travelLiveData;
    }

    public MutableLiveData<List<UserInfoBean>> getUserLiveData() {
        if (userLiveData == null) {
            userLiveData = new MutableLiveData<>();
        }
        return userLiveData;
    }

    /**
     * 获得搜索详情
     * @param type 0-基地 1-游记 2-团队 3-动态 4-用户
     * @param string 搜索关键字
     */
    public void getSearchDetailLiveDataServer(int type, String string, int page, int id) {

        Network.getSearchDetail(type, string,page, id,new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = Objects.requireNonNull(res.body()).string();
                if (type == 0) {
                    ResultBean<List<BaseBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, BaseBean.class);
                    if (resultBean.getCode().equals(200)) {
                        getBaseLiveData().postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                } else if (type == 1) {
                    ResultBean<List<TravelsSimpleBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, TravelsSimpleBean.class);
                    if (resultBean.getCode().equals(200)) {
                        getTravelLiveData().postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                } else if (type == 2) {
                    ResultBean<List<TeamSimpleBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, TeamSimpleBean.class);
                    if (resultBean.getCode().equals(200)) {
                        getTeamLiveData().postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                } else if (type == 3) {
                    ResultBean<List<DynamicBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, DynamicBean.class);
                    if (resultBean.getCode().equals(200)) {
                        getDynamicLiveData().postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                } else if (type == 4) {
                    ResultBean<List<UserInfoBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, UserInfoBean.class);
                    if (resultBean.getCode().equals(200)) {
                        getUserLiveData().postValue(resultBean.getData());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }
}
